<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <meta name="viewport"
        content="maximum-scale=1.0,minimum-scale=1.0,user-scalable=0,width=device-width,initial-scale=1.0" />
    <meta name="format-detection" content="telephone=no,email=no,date=no,address=no">
    <link rel="stylesheet" type="text/css" href="../css/aui.2.0.css" />
    <link rel="stylesheet" type="text/css" href="../css/aui-flex.css" />
    <style>
    </style>
</head>
<style>
    .home:before {
        content: url(../image/home.png);
    }

    .refresh:before {
        content: url(../image/refresh.png);
    }

    .company:before {
        content: url(../image/company.png);
    }

    .pa-title {
        font-size: 17px;
        color: black;
        font-weight: bold;
    }

    .pa-title-content {
        font-size: 17px;
        color: black;
    }

    body {
        background-color: #D9D8DB;
    }


    .pa_xg_defect_t {
        background-color: #254060;
        height: 385px;
        color: white;
        overflow: auto;
    }

    .pa_xg_defect {
        margin-top: 5px;
        height: 40px;
        line-height: 40px;
        margin-left: 2%;
        text-align: center;
        color: white;
    }

    .pa_xg_defect input {
        height: 40px;
        min-height: 40px;
    }

    .pa_xg_defect_but {
        height: 50px;
        line-height: 50px;
        margin-left: 50px;
        float: left;
        width: 130px;
        font-size: 27px;
        color: white;
        text-align: center;
    }

    p {
        font-size: 17px;
        color: black;
        font-weight: bold;
        text-align: center;
    }

    table {
        width: 100%;
        table-layout: fixed;
        word-wrap: break-word;
    }

    td {
        white-space: nowrap;
        text-overflow: ellipsis;
        overflow: hidden;
    }

    td input {
        text-align: center;
    }
</style>

<body>
    <header class="aui-bar aui-bar-nav" style="background-color: #D9D8DB">
        <div style="width: 10%;height: 60px;float: left;padding-top: 6px;">
            <a class="aui-pull-left aui-btn ">
                <span class="aui-iconfont company"></span>
            </a>
        </div>
        <div style="width: 10%;height: 60px;float: left;">
            <div style="text-align: left;">
                <span class="pa-title">富晟安创</span>
            </div>
        </div>
        <div style="width: 70%;height: 60px;float: left;">
            <div style="width:18%; float: left; text-align: left;">
                <span class="pa-title-content" id="locationName"></span>
            </div>
            <div style="width:18%; float: left; text-align: left;">
                <span class="pa-title-content" id="workCenterName"></span>
            </div>
            <div style="width:24%; float: left; text-align: left;">
                <span class="pa-title-content" id="workCellName"></span>
            </div>
            <div style="width:40%; float: left; text-align: left;">
                <span class="pa-title-content" id="nowDate"></span>
            </div>
        </div>
        <div style="width: 5%;height: 50px;float: left;">
        </div>
        <div style="width: 5%;height: 50px;float: left;">
            <a class="aui-pull-right aui-btn" onclick="closeWin()">
                <span class="aui-iconfont home"></span>
            </a>
        </div>
    </header>
    <div class="aui-content">
        <div class="aui-flex-col aui-flex-center" style="height: 30px;">
            <div class="aui-flex-item-12">
                <p>下挂_录入不合格数</p>
            </div>
        </div>

        <div class="aui-flex-col aui-flex-center">
            <div class="aui-flex-item-4">
                <div class="aui-list-item-inner" style="border: 0;margin-left: 10px;margin-top: 5px;">
                    <div class="aui-list-item-label" style="font-size: 25px;float: left;">
                        <span>不合格数：</span>
                        <span id="unQualifyQty"></span>
                    </div>
                </div>
            </div>
            <div class="aui-flex-item-8">
                <div class="pa_xg_defect_but" style="background-color: #F59A23;" onclick="split()">
                    拆分
                </div>

                <div class="pa_xg_defect_but" style="background-color: #528302;" onclick="save()">
                    提交
                </div>

                <div class="pa_xg_defect_but" style="background-color: #D9001B;" onclick="cancle()">
                    关闭
                </div>
            </div>
        </div>
        <div class="aui-flex-col aui-flex-center" style="margin-top: 1%;">
            <div class="aui-flex-item-12">
                <div class="aui-flex-col pa_xg_defect_t">
                    <div class="aui-flex-item-12">
                        <div style="height: 100%;width: 100%;">
                            <table class="table" border="1px solid gray" cellspacing="0" cellpadding="0">
                                <thead>
                                    <tr style="text-align: center;color: white;font-size: 16px;">
                                        <td algin='center' style="width:4%"></td>
                                        <td algin='center' style="width:6%">序号</td>
                                        <td algin='center' style="width:15%">容器码</td>
                                        <td algin='center' style="width:14%">物料号</td>
                                        <td algin='center' style="width:15%">物料名</td>
                                        <td algin='center' style="width:10%">批次号</td>
                                        <td algin='center' style="width:10%">不合格数</td>
                                        <td algin='center' style="width:13%">缺陷类型</td>
                                        <td algin='center' style="width:13%">缺陷名称</td>
                                    </tr>
                                </thead>
                                <tbody id="hangDownDefectDataHtml">
                                </tbody>
                            </table>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</body>
<script type="text/javascript" src="../script/api.js"></script>
<script type="text/javascript" src="../script/jquery-1.8.2.min.js"></script>
<script type="text/javascript" src="../script/public.js"></script>
<script type="text/javascript">

    var equipUniqueId = "";
    var tableList = [];
    var chkItemTypeHtml = "";

    //初始化
    apiready = function () {
        //获取设备唯一号
        equipUniqueId = localStorage.getItem('equipUniqueId');
        //产线工位信息
        document.getElementById("locationName").innerHTML = api.pageParam.locationName;
        document.getElementById("workCenterName").innerHTML = api.pageParam.workCenterName;
        document.getElementById("workCellName").innerHTML = api.pageParam.workCellName;
        document.getElementById("nowDate").innerHTML = getTimeCommon();
        //当前时间刷新
        window.setInterval("getTime()", 1000);

        //查询缺陷类别下拉框
        doSearchCombox();
    }

    //时间刷新
    function getTime() {
        document.getElementById("nowDate").innerHTML = getTimeCommon();
    }

    //缺陷类别下拉框
    function doSearchCombox() {
        var un = localStorage.getItem('un');
        var serverUrl = localStorage.getItem('serverUrl');
        var url = serverUrl + "/app/getUtilComboxList";
        var data = {
            values: {
                tablename: "qc_bd_chk_item_type",
                column_name: "name",
                column_value: "sysid",
                condition: "is_delete=0",
                un: un,
            }
        };
        var params = new Object();
        params.url = url;
        params.dataType = 'json';
        params.method = 'post';

        ajaxForData(params, data, false, function (ret, err) {
            if (ret) {
                var data = JSON.stringify(ret);
                var myobj = eval("(" + data + ")");

                //成功
                if (myobj.code == 0) {
                    var list = eval(myobj.list);
                    var html = "";
                    //遍历
                    for (var i = 0; i < list.length; i++) {
                        html += "<option value='" + list[i].value + "'>" + list[i].label + "</option>";
                    }

                    chkItemTypeHtml = html;
                    //查询下挂记录
                    doSearch();
                } else {
                    api.toast({
                        msg: myobj.message,
                        duration: 3000
                    });
                }
            } else {
                alert("网络连接超时!")
            }
        });
    }

    //查询下挂-不合格记录
    function doSearch() {
        var un = localStorage.getItem('un');
        var serverUrl = localStorage.getItem('serverUrl');
        var url = serverUrl + "/app/getAssemblyHangUpData";
        var data = {
            values: {
                fbCode: api.pageParam.fbCode,
                workOrderCode: api.pageParam.workOrderCode,
                state: 1, //状态：0-上挂，1-下挂
                un: un,
            }
        };
        var params = new Object();
        params.url = url;
        params.dataType = 'json';
        params.method = 'post';

        ajaxForData(params, data, false, function (ret, err) {
            if (ret) {
                var data = JSON.stringify(ret);
                var myobj = eval("(" + data + ")");

                //成功
                if (myobj.code == 0) {
                    var list = eval(myobj.list);
                    var html = "";

                    if (list != null && list.length > 0) {
                        document.getElementById("unQualifyQty").innerHTML = myobj.unQualifyQty;

                        var html = "";

                        var chkItemType = "chkItemType";
                        var chkItem = "chkItem";

                        //遍历
                        for (var i = 0; i < list.length; i++) {
                            var chkItemType = "chkItemType_" + i;
                            var chkItem = "chkItem_" + i;
                            var unQualifyQtyId = "unQualifyQty_" + i;

                            html += "<tr style='text-align: center;color: white;font-size: 14px;height:30px;'>";
                            html += "<td><input name='sub' type='checkbox' /></td>";
                            html += "<td name='index'>" + (i + 1) + "</td>";
                            html += "<td name='ctBarCode'>" + list[i].ctBarCode + "</td>";
                            html += "<td name='sitmCode'>" + list[i].sitmCode + "</td>";
                            html += "<td name='sitmName'>" + list[i].sitmName + "</td>";
                            html += "<td name='lotCode'>" + list[i].lotCode + "</td>";
                            html += "<td name='unQualifyQty'><span id=\"" + unQualifyQtyId + "\" >" + list[i].unQualifyQty + "</span></td>";
                            html += "<td name='chkItemType'><select id = \"" + chkItemType + "\" style='font-size:14px;color:white;height:30px;min-height:30px;line-height:30px;text-align:center;' onchange='onChange(\"" + chkItemType + "\" ,\"" + chkItem + "\")'>" + chkItemTypeHtml + "</select></td>";
                            html += "<td name='chkItem'><select style='font-size:14px;color:white;height:30px;line-height:30px;min-height:30px;text-align:center;' id = \"" + chkItem + "\" style=''></select></td>";
                            html += "</tr>";
                        }

                        $api.html($api.byId("hangDownDefectDataHtml"), html);
                        tableList = list;
                    } else {

                        //清空表单
                        clearForm();
                    }

                } else {
                    //清空表单
                    clearForm();
                    api.toast({
                        msg: myobj.message,
                        duration: 3000
                    });
                }
            } else {
                alert("网络连接超时!");
            }
        });
    }

    //缺陷类别值改变
    function onChange(chkItemType, chkItem) {
        var chkItemTypeId = $("#" + chkItemType).val();
        var un = localStorage.getItem('un');
        var serverUrl = localStorage.getItem('serverUrl');
        var url = serverUrl + "/app/getUtilComboxList";
        var data = {
            values: {
                tablename: "qc_qual_chk_item",
                column_name: "name",
                column_value: "sysid",
                condition: "chk_item_type_gid= '" + chkItemTypeId + "'",
                un: un,
            }
        };
        var params = new Object();
        params.url = url;
        params.dataType = 'json';
        params.method = 'post';

        ajaxForData(params, data, false, function (ret, err) {
            if (ret) {
                var data = JSON.stringify(ret);
                var myobj = eval("(" + data + ")");

                //成功
                if (myobj.code == 0) {
                    var list = eval(myobj.list);
                    var html = "";
                    //遍历
                    for (var i = 0; i < list.length; i++) {
                        html += "<option value='" + list[i].value + "'>" + list[i].label + "</option>";
                    }

                    $api.html($api.byId(chkItem), html);
                } else {
                    $api.html($api.byId(chkItem), "");
                    api.toast({
                        msg: myobj.message,
                        duration: 3000
                    });
                }
            } else {
                alert("网络连接超时!")
            }
        });
    }

    //清空表单
    function clearForm() {
        document.getElementById("unQualifyQty").innerHTML = "0";
        $api.html($api.byId("hangDownDefectDataHtml"), "");
        tableList = [];
    }

    //拆分
    function split() {
        //选中数据
        var check = $("input[type='checkbox']:checked");
        var count = 0;
        var unQualifyQty = 0;
        var index = 0;
        var row;
        check.each(function () { //遍历
            row = $(this).parent("td").parent("tr");
            index = row.find("[name='index']").html();
            count++;
        });

        if (count != 1) {
            api.toast({
                msg: '请选择一条表格数据！',
                duration: 3000
            });
            return;
        }
        unQualifyQty = $("#unQualifyQty_" + (index - 1)).html();
        if (parseInt(unQualifyQty) == 1) {
            api.toast({
                msg: '不合格数量为【1】，不允许拆分！',
                duration: 3000
            });
            return;
        }

        var dialogBox = api.require('dialogBox');
        dialogBox.input({
            keyboardType: 'default',
            texts: {
                title: "拆分",
                placeholder: '录入数量',
                leftBtnTitle: '取消',
                rightBtnTitle: '确定'
            },
            styles: {
                bg: '#fff',
                corner: 2,
                w: 300,
                h: 240,
                title: {
                    h: 60,
                    alignment: 'center',
                    size: 14,
                    color: '#000',
                    marginT: 30,
                },
                input: {
                    h: 60,
                    y: 30,
                    marginT: 15,
                    marginLeft: 10,
                    marginRight: 10,
                    textSize: 20,
                    textColor: '#000',
                    corner: 5,                  //（可选项）数字类型；对输入框圆角大小；默认：0

                },
                dividingLine: {
                    width: 0.5,
                    color: '#696969'
                },
                left: {
                    bg: 'rgba(0,0,0,0)',
                    color: '#007FFF',
                    size: 12,
                    h: 40,                //(可选项) 数字类型；右边按钮的高度；默认：40
                    w: 60,                   //(可选项) 数字类型；左边按钮的宽度；默认：提示框的宽度的一半
                    marginB: 10,            //(可选项) 数字类型；左边按离底部的边距；默认：0
                    corner: 5,              //(可选项) 数字类型；左边按钮的圆角；默认：0
                    widhtBorder: 1,         //(可选项) 数字类型；左边按钮的边宽；默认：0
                    normalBorderColor: '#778899', //(可选项) 数字类型；左边按钮的边宽颜色；默认：'rgba(0,0,0,0)'
                    activeBorderColor: '#0000CD', //(可选项) 数字类型；左边按钮的高亮状态下边宽颜色；默认：'rgba(0,0,0,0)'
                    activeColor: '#006400', //(可选项) 数字类型；左边按钮的高亮状态下标题颜色；默认：'rgba(0,0,0,0)'
                    activeBg: '#6B8E23'   //(可选项) 数字类型；左边按钮的高亮状态下背景颜色；默认：'rgba(0,0,0,0)'
                },
                right: {
                    bg: 'rgba(0,0,0,0)',
                    color: '#007FFF',
                    size: 12,
                    h: 40,                //(可选项) 数字类型；右边按钮的高度；默认：40
                    w: 60,                   //(可选项) 数字类型；右边按钮的宽度；默认：提示框的宽度的一半
                    marginB: 10,            //(可选项) 数字类型；右边按离底部的边距；默认：0
                    corner: 5,              //(可选项) 数字类型；右边按钮的圆角；默认：0
                    widhtBorder: 1,         //(可选项) 数字类型；右边按钮的边宽；默认：0
                    normalBorderColor: '#778899', //(可选项) 数字类型；右边按钮的边宽颜色；默认：'rgba(0,0,0,0)'
                    activeBorderColor: '#0000CD', //(可选项) 数字类型；右边按钮的高亮状态下边宽颜色；默认：'rgba(0,0,0,0)'
                    activeColor: '#006400', //(可选项) 数字类型；右边按钮的高亮状态下标题颜色；默认：'rgba(0,0,0,0)'
                    activeBg: '#6B8E23'   //(可选项) 数字类型；右边按钮的高亮状态下背景颜色；默认：'rgba(0,0,0,0)'
                }
            }
        }, function (ret) {
            //数量
            var splitQty = ret.text;

            //取消
            if (ret.eventType == 'left') {
                dialogBox.close({
                    dialogName: 'input'
                });

                //确定
            } else {

                //卡号必输校验
                if (splitQty == null || splitQty == "" || splitQty == undefined) {
                    api.toast({
                        msg: "请录入数量!",
                        duration: 3000
                    });
                } else if (parseInt(splitQty) <= 0) {

                    api.toast({
                        msg: "数量应大于【0】!",
                        duration: 3000
                    });
                } else if (parseInt(splitQty) >= parseInt(unQualifyQty)) {
                    api.toast({
                        msg: "数量应小于【" + unQualifyQty + "】!",
                        duration: 3000
                    });
                } else {
                    dialogBox.close({
                        dialogName: 'input'
                    });
                    //拆分
                    addRow(unQualifyQty, splitQty, index);
                }
            }
        });
    }

    //表格新增行
    function addRow(unQualifyQty, splitQty, index) {
        $("#unQualifyQty_" + (index - 1)).html(parseInt(unQualifyQty) - parseInt(splitQty));
        var unQualifyQtyId = "unQualifyQty_" + tableList.length;
        var chkItemType = "chkItemType_" + tableList.length;
        var chkItem = "chkItem_" + tableList.length;
        var map = tableList[index - 1];
        map.unQualifyQty = splitQty;
        var html = "";
        html += "<tr style='text-align: center;color: white;font-size: 14px;height:30px;'>";
        html += "<td><input name='sub' type='checkbox' /></td>";
        html += "<td name='index'>" + (tableList.length + 1) + "</td>";
        html += "<td name='ctBarCode'>" + map.ctBarCode + "</td>";
        html += "<td name='sitmCode'>" + map.sitmCode + "</td>";
        html += "<td name='sitmName'>" + map.sitmName + "</td>";
        html += "<td name='lotCode'>" + map.lotCode + "</td>";
        html += "<td name='unQualifyQty'><span id=\"" + unQualifyQtyId + "\" >" + splitQty + "</span></td>";
        html += "<td name='chkItemType'><select id = \"" + chkItemType + "\" style='font-size:14px;color:white;height:30px;min-height:30px;line-height:30px;text-align:center;' onchange='onChange(\"" + chkItemType + "\" ,\"" + chkItem + "\")'>" + chkItemTypeHtml + "</select></td>";
        html += "<td name='chkItem'><select style='font-size:14px;color:white;height:30px;line-height:30px;min-height:30px;text-align:center;' id = \"" + chkItem + "\" style=''></select></td>";
        html += "</tr>";
        $api.append($api.byId("hangDownDefectDataHtml"), html);
        tableList.push(map);
    }

    //保存
    function save() {
        var chkItemType;
        var chkItem;
        var unQualifyQty;
        var josnList = [];
        if (tableList == null || tableList.length == 0) {
            api.toast({
                msg: "没有需要保存的数据！",
                duration: 3000
            });
            return;
        }

        //遍历
        for (var i = 0; i < tableList.length; i++) {
            chkItemType = $("#chkItemType_" + i).val();
            //缺陷列表非空校验
            if (chkItemType == null || chkItemType == "" || chkItemType == undefined) {
                api.toast({
                    msg: "第【" + (i + 1) + "】行缺陷类别不能为空！",
                    duration: 3000
                });
                return;
            }
            chkItem = $("#chkItem_" + i).val();
            //缺陷名称非空校验
            if (chkItem == null || chkItem == "" || chkItem == undefined) {
                api.toast({
                    msg: "第【" + (i + 1) + "】行缺陷名称不能为空！",
                    duration: 3000
                });
                return;
            }
            unQualifyQty = $("#unQualifyQty_" + i).html();
            josnList.push({
                ctBarCode: tableList[i].ctBarCode,
                sitmCode: tableList[i].sitmCode,
                sitmName: tableList[i].sitmName,
                lotCode: tableList[i].lotCode,
                unQualifyQty: unQualifyQty,
                chkItemType: chkItemType,
                chkItem: chkItem
            });
        }

        var un = localStorage.getItem('un');
        var serverUrl = localStorage.getItem('serverUrl');
        var url = serverUrl + "/app/getAssemblyHangDownQxSave";
        var data = {
            values: {
                josnList: JSON.stringify(josnList),
                clientCode: equipUniqueId,
                fbCode: api.pageParam.fbCode,
                workOrderCode: api.pageParam.workOrderCode,
                un: un,
            }
        };

        var params = new Object();
        params.url = url;
        params.dataType = 'json';
        params.method = 'post';

        api.showProgress({
            title: '保存中...',
        });

        ajaxForData(params, data, false, function (ret, err) {
            if (ret) {
                api.hideProgress();
                var data = JSON.stringify(ret);
                var myobj = eval("(" + data + ")");

                //成功
                if (myobj.code == 0) {

                    api.toast({
                        msg: "保存成功!",
                        duration: 3000
                    });

                    //返回上一级
                    cancle();
                } else {
                    api.toast({
                        msg: myobj.message,
                        duration: 3000
                    });
                }
            } else {
                api.hideProgress();
                alert("网络连接超时!");
            }
        });
    }

    //取消按钮
    function cancle() {
        api.closeWin({});
        api.execScript({
            name: api.pageParam.name,
            script: 'refresh()'
        });
    }

    //返回按钮
    function closeWin() {
        api.closeToWin({ name: api.pageParam.rootName });
        api.execScript({
            name: api.pageParam.rootName,
            script: 'refresh()'
        });
    }
</script>

</html>